package trace

import (
	"go-toolkit/library/net/trace/tools"
	"math"
)

type rateLimitSampler struct {
	maxTracesPerSecond float64
	rateLimiter        *tools.ReconfigurableRateLimiter
}

func newRateLimitSampler(maxTracesPerSecond float64) sampler {
	return &rateLimitSampler{
		maxTracesPerSecond: maxTracesPerSecond,
		rateLimiter:        tools.NewRateLimiter(maxTracesPerSecond, math.Max(maxTracesPerSecond, 1.0)),
	}
}

func (p *rateLimitSampler) IsSampled(traceID uint64, operationName string) (bool, float64) {
	return p.rateLimiter.CheckCredit(1.0), p.maxTracesPerSecond
}

func (p *rateLimitSampler) Close() error {
	return nil
}
